package com.mju.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mju.controller.common.BaseController;
import com.mju.controller.common.JsonResult;
import com.mju.pojo.RefundInfo;
import com.mju.service.IRefundInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/refundInfo")
@Api(tags = "退款信息服务")
public class RefundInfoController extends BaseController {
    @Autowired
    private IRefundInfoService refundInfoService;
    //记录退款信息
    @ApiOperation("记录退款信息")
    @GetMapping("/createrRefund/{oid}/{reason}")
    public JsonResult<RefundInfo> createrRefundByOrder(@PathVariable Integer oid,@PathVariable String reason){
        log.info("开始记录退款信息...");
        RefundInfo data = refundInfoService.createrRefundByOrder(oid, reason);
        return new JsonResult<>(OK,data);
    }
    //更新退款信息(补全信息)
    @ApiOperation("更新退款信息")
    @PostMapping("/updateRefund")
    public JsonResult updateRefund(
            @RequestParam(value = "refundNo") String refundNo,@RequestParam(value = "tradeNo",required = false)String tradeNo,
            @RequestParam("content")String content,@RequestParam("refundStatus") String refundStatus){
        log.info("开始更新退款信息...");
        refundInfoService.updateRefund(refundNo,tradeNo,content,refundStatus);
        return new JsonResult(OK,"更新退款信息成功");
    }

    @ApiOperation("无条件分页查询所有退款信息")
    @GetMapping("/{currentPage}/{pageSize}")
    public JsonResult<IPage<RefundInfo>> queryAllRefundInfo(
            @PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize){
        IPage<RefundInfo> data = refundInfoService.queryAllRefundInfo(currentPage, pageSize);
        return new JsonResult<>(OK,data);
    }

    @ApiOperation("条件分页查询所有退款信息,条件最多包含订单号，退款订单号，支付宝退款订单号，原交易金额，退款状态")
    @PostMapping("/conQueryAllRefundInfo")
    public JsonResult<IPage<RefundInfo>> conQueryAllRefundInfo(
            @RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pageSize,@RequestBody RefundInfo refundInfo){
        IPage<RefundInfo> data = refundInfoService.conQueryAllRefundInfo(currentPage, pageSize,refundInfo);
        //如果当前页码值大于了总页码值，那么重新执行查询操作，使用最大页码值作为当前页码值
        if( currentPage > data.getPages()){
            data = refundInfoService.conQueryAllRefundInfo((int)data.getPages(), pageSize,refundInfo);
        }
        return new JsonResult<>(OK,data);
    }
}
